﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace vmMachine
{
    class VendingMachineLogic : VendingMachine
    {
        private int drinkSelectedFlag;
        //if drinkSelectedFlag ==1 --> drink Sold out
        //if drinkSelectedFlag ==2 --> dispence drink
        //if drinkSelectedFlag ==3 --> not enough money

        private Boolean giveChangeEventFlag=true;

        public VendingMachineLogic(Form1 form)
            : base(form)
        { }
        
        public void drinkSelected(int drinkNum)
        {
            if (items[drinkNum - 1].isInStock() == false)
            {
                this.MachineDisplay = "SOLD";
                this.drinkSelectedFlag = 1;
                notify(1);
                return;
            }
            else if (this.MoneyAmount >= this.items[drinkNum - 1].Price)
            {
                items[drinkNum - 1].dispence();
                this.GivenChange = this.MoneyAmount - this.items[drinkNum - 1].Price;
                this.MoneyAmount = 0;
                drinkSelectedFlag = 2;
                giveChangeEventFlag = false; //change button WAS NOT clicked
            }
            else
            {
                drinkSelectedFlag = 3;
            }
            //string value = this.items[drinkNum-1].Price.ToString();
            this.MachineDisplay = String.Format("{0:00.00}", this.items[drinkNum - 1].Price);
            //this.MachineDisplay = value[0]+value[1]+value[2]+value[3]+value[4]+"";
            //add call to display
            notify(1);
            return;
        }


        public void coinInserted(double amount)
        {
            this.MoneyAmount += amount;
            this.MachineDisplay = this.MoneyToString();
            notify(3);
            return;
        }

        public int changeButtonClicked()
        {
            giveChangeEventFlag = true;
            GivenChange = MoneyAmount;
            this.resetMoneyAmount();
            MachineDisplay = MoneyToString();

            notify(2);
            return 0;
        }

        public string MoneyToString()
        {
            string moneyAsString = String.Format("{0:00.00}", MoneyAmount);
            return moneyAsString;
        }

        public string changeToString()
        {
            string changeAsString = String.Format("{0:00.00}", GivenChange);
            return changeAsString;
        }

        public void resetChange()
        {
            this.GivenChange = 0;
        }

        public void resetMoneyAmount()
        {
            this.MoneyAmount = 0;
        }

        public Boolean GiveChangeEventFlag
        {
            get
            {
                return giveChangeEventFlag;
            }
        }

        public int DrinkSelectedFlag
        {
            get
            {
                return drinkSelectedFlag;
            }
        }


    }
}
